﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Data;

namespace FastDeal.Control
{
    public partial class wuc_QuanLyDiaPhuong : System.Web.UI.UserControl
    {
        DiaPhuong dp = new DiaPhuong();
        DiaPhuong_BUS db = new DiaPhuong_BUS();
        protected void Page_Load(object sender, EventArgs e)
        {
                if (!IsPostBack)
                {
                    LayDanhSachDiaPhuong();
                }
        }

        public void LayDanhSachDiaPhuong()
        {
            List<DiaPhuong> dpl = new List<DiaPhuong>();
            dpl = db.LayDanhSach_DiaPhuong();
            gvDanhSachDiaPhuong.DataSource = dpl;
            gvDanhSachDiaPhuong.DataBind();
        }

        protected void gvDanhSachDiaPhuong_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachDiaPhuong.EditIndex = e.NewEditIndex;
            LayDanhSachDiaPhuong();
        }

        protected void gvDanhSachDiaPhuong_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int madp = int.Parse(((Label)gvDanhSachDiaPhuong.Rows[e.RowIndex].Cells[0].FindControl("Label3")).Text);
            string tendp = ((TextBox)gvDanhSachDiaPhuong.Rows[e.RowIndex].Cells[1].FindControl("txt1")).Text;
            dp.MaDiaPhuong = madp;
            dp.TenDiaPhuong = tendp;
            db.Sua_DiaPhuong(dp);
            gvDanhSachDiaPhuong.EditIndex = -1;
            Response.Redirect(Request.Url.ToString());
            LayDanhSachDiaPhuong();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtDiaPhuong.Text == "")
                {
                    lbloi.Text = "Bạn Chưa Nhập Liệu - Mời Nhập Lại";
                }
                else
                {
                    dp.TenDiaPhuong = txtDiaPhuong.Text;
                    db.Them_DiaPhuong(dp);
                    Response.Redirect(Request.Url.ToString());
                    LayDanhSachDiaPhuong();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message); 
            }
        }

        protected void gvDanhSachDiaPhuong_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachDiaPhuong.EditIndex = -1;
            LayDanhSachDiaPhuong();
        }
    }
}